package gwt.bbsbot.server.twitter;

import gwt.bbsbot.server.twitter.beans.TweetBean;
import gwt.bbsbot.server.twitter.constant.TwitterConstant;

import java.util.ArrayList;
import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;

public class TwitterManager {

	private Twitter twitter;
	private String whoami;
	
	public TwitterManager() {
		this.twitter = new Twitter(TwitterConstant.ID_TEST, TwitterConstant.PASS_TEST);
		this.whoami = TwitterConstant.ID_TEST;
	}
	public TwitterManager(String id, String pass) {
		this.twitter = new Twitter(id, pass);
		this.whoami = id;
	}

	public String getProfileImageURL(String name){
		try {
			List<User> users = this.twitter.getFollowersStatuses();
			for(User user:users){
				if( user.getScreenName().equals(name) || user.getName().equals(name) ){
					return user.getProfileImageURL().toString();
				}
			}
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public List<TweetBean> search(){
		List<TweetBean> list = new ArrayList<TweetBean>();
		try {
			List<Status> listStatus = this.twitter.getMentions();
			for( Status status:listStatus){
				status.getUser().getProfileImageURL();
				list.add(new TweetBean(
									this.whoami,
									status.getUser().getScreenName(), 
									status.getText(),
									status.getUser().getProfileImageURL().toString()
									));
			}
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
	
	public void say(TweetBean bean){
		try {
			twitter.updateStatus(bean.toString());
		} catch (TwitterException e) {
			e.printStackTrace();
		}

	}
}
